<template>
  <div class="register-container">
    <div class="register-card">
      <h2 class="register-title">用户注册</h2>
      
      <form @submit.prevent="handleRegister" class="register-form">
        <!-- 用户名输入 -->
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="form.username"
            required
            placeholder="请输入用户名"
            class="form-input"
          >
        </div>
        
        <!-- 密码输入 -->
        <div class="form-group">
          <label for="password">密码</label>
          <div class="password-input">
            <input
              :type="showPassword ? 'text' : 'password'"
              id="password"
              v-model="form.password"
              required
              placeholder="请输入密码"
              class="form-input"
            >
            <button
              type="button"
              @click="showPassword = !showPassword"
              class="toggle-btn"
            >
              <i :class="showPassword ? 'fa fa-eye-slash' : 'fa fa-eye'"></i>
            </button>
          </div>
        </div>
        
        <!-- 确认密码输入 -->
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <div class="password-input">
            <input
              :type="showConfirmPassword ? 'text' : 'password'"
              id="confirmPassword"
              v-model="form.confirmPassword"
              required
              placeholder="请再次输入密码"
              class="form-input"
            >
            <button
              type="button"
              @click="showConfirmPassword = !showConfirmPassword"
              class="toggle-btn"
            >
              <i :class="showConfirmPassword ? 'fa fa-eye-slash' : 'fa fa-eye'"></i>
            </button>
          </div>
          <div v-if="form.password !== form.confirmPassword && form.confirmPassword" class="password-match-error">
            两次输入的密码不一致
          </div>
        </div>
        
        <!-- 身份选择 -->
        <div class="form-group">
          <label>身份选择</label>
          <div class="identity-options">
            <label class="radio-option">
              <input
                type="radio"
                v-model="form.identity"
                value="1"
                required
              >
              <span>商家</span>
            </label>
            <label class="radio-option">
              <input
                type="radio"
                v-model="form.identity"
                value="2"
              >
              <span>骑手</span>
            </label>
            <label class="radio-option">
              <input
                type="radio"
                v-model="form.identity"
                value="3"
              >
              <span>用户</span>
            </label>
          </div>
        </div>
        
        <!-- 验证码 -->
        <div class="form-group">
          <label for="verification">验证码</label>
          <div class="verification-container">
            <input
              type="text"
              id="verification"
              v-model="form.verification"
              required
              placeholder="请输入验证码"
              class="form-input verification-input"
            >
            <img 
              :src="verificationUrl" 
              alt="验证码" 
              class="verification-img"
              @click="refreshVerification"
              title="点击刷新"
            >
          </div>
        </div>
        
        <!-- 错误提示 -->
        <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
        
        <!-- 注册按钮 -->
        <button 
          type="submit" 
          class="register-btn"
          :disabled="isLoading || form.password !== form.confirmPassword"
        >
          <span v-if="isLoading">注册中...</span>
          <span v-else>注册</span>
        </button>
        
        <!-- 前往登录按钮 -->
        <button 
          type="button" 
          class="login-btn"
          @click="$router.push('/login')"
        >
          已有账号？去登录
        </button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
import { v4 as uuidv4 } from 'uuid';

// 路由实例
const router = useRouter();

// 表单数据
const form = ref({
  username: '',
  password: '',
  confirmPassword: '',
  identity: '',
  verification: ''
});

// 状态管理
const showPassword = ref(false);
const showConfirmPassword = ref(false);
const isLoading = ref(false);
const errorMessage = ref('');
const verificationUuid = ref('');
const verificationUrl = ref('');

// 生成验证码UUID并获取验证码图片
const refreshVerification = () => {
  verificationUuid.value = uuidv4();
  verificationUrl.value = `/api/user/verification/register/${verificationUuid.value}/`;
};

// 注册处理
const handleRegister = async () => {
  // 表单验证
  if (form.value.password !== form.value.confirmPassword) {
    errorMessage.value = '两次输入的密码不一致';
    return;
  }
  
  if (!form.value.identity) {
    errorMessage.value = '请选择身份';
    return;
  }
  
  isLoading.value = true;
  errorMessage.value = '';
  
  try {
    // 发送注册请求
    await axios.post('/user/user/', {
      username: form.value.username,
      password: form.value.password,
      identity: form.value.identity,
      verify: form.value.verification,
      uuid: verificationUuid.value
    });
    
    // 注册成功，跳转到登录页
    router.push({
      path: '/login',
      query: { registered: 'true' }
    });
  } catch (err) {
    // 处理错误
    errorMessage.value = err.response?.data?.message || '注册失败，请检查信息是否正确';
    // 刷新验证码
    refreshVerification();
  } finally {
    isLoading.value = false;
  }
};

// 页面加载时获取验证码
onMounted(() => {
  refreshVerification();
});
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #fff;
  padding: 20px;
}

.register-card {
  width: 100%;
  max-width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  border: 1px solid #f8d7da;
}

.register-title {
  text-align: center;
  color: #dc3545; /* 红色标题 */
  margin-bottom: 25px;
  font-size: 24px;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-input:focus {
  outline: none;
  border-color: #dc3545; /* 红色焦点边框 */
}

.password-input {
  position: relative;
}

.toggle-btn {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #888;
  cursor: pointer;
}

.password-match-error {
  color: #dc3545;
  font-size: 14px;
  margin-top: 5px;
}

.identity-options {
  display: flex;
  gap: 20px;
  margin-top: 10px;
}

.radio-option {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

.verification-container {
  display: flex;
  gap: 10px;
}

.verification-input {
  flex: 1;
}

.verification-img {
  width: 140px;
  height: 40px;
  object-fit: cover;
  cursor: pointer;
  border-radius: 4px;
  border: 1px solid #ddd;
}

.error-message {
  color: #dc3545; /* 红色错误信息 */
  text-align: center;
  margin: 15px 0;
  padding: 10px;
  background-color: #f8d7da;
  border-radius: 4px;
}

.register-btn {
  width: 100%;
  padding: 12px;
  background-color: #dc3545; /* 红色注册按钮 */
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s;
}

.register-btn:hover {
  background-color: #bb2d3b;
}

.register-btn:disabled {
  background-color: #d9534f;
  opacity: 0.7;
  cursor: not-allowed;
}

.login-btn {
  width: 100%;
  margin-top: 15px;
  padding: 12px;
  background-color: white; /* 白色登录按钮 */
  color: #dc3545; /* 红色文字 */
  border: 1px solid #dc3545;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.login-btn:hover {
  background-color: #f8d7da;
}
</style>
    